package com.uenpay.im.ui.old.activity.login;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.cache.HttpStreamCache;
import com.qiyunxin.android.http.listener.OnHttpRequestTextListener;
import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.qiyunxin.android.http.net.HttpRequestAsyncTaskQueue;
import com.qiyunxin.android.http.net.HttpRequestType;
import com.qiyunxin.android.http.net.HttpTextAsyncTask;
import com.qyx.android.message.type.PacketType;
import com.socks.library.KLog;
import com.uenpay.im.IMApplication;
import com.uenpay.im.ui.old.activity.stickers.StickersHandle;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.QYXUserEntity;
import com.uenpay.im.invokeitems.HttpRequestHandle;
import com.uenpay.im.invokeitems.login.CheckMobileInvokeItem;
import com.uenpay.im.invokeitems.login.ForgetPwdInvokeItem;
import com.uenpay.im.invokeitems.login.LoginInvokItem;
import com.uenpay.im.invokeitems.login.RegisterInvokeItme;
import com.uenpay.im.invokeitems.login.ResendCaptchaInvokeItem;

/**
 * @author SL
 * @created 2016-1-27 下午2:56:45
 * @copyRight http://www.qiyunxin.com
 * @function 登录注册管理
 */
public class LoginAndRegisterHandle {

	public interface ILoginResultListener {
		void onLoginResult(int status, String msg, QYXUserEntity mQyxUserEntity);
	}

	/**
	 * 登录
	 *
	 * @param account
	 * @param pwd
	 * @param mILoginResultListener
	 */
	public void loginApp(final String account, String pwd, final ILoginResultListener mILoginResultListener) {
		LoginInvokItem item = new LoginInvokItem(account, pwd);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY, new HttpRequestHandle.IRequestResultListener() {

			@Override
			public void onResult(Object resultObject, boolean isCache, String failReason) {
				if (resultObject != null) {
					IMApplication.getInstance().regDeviceNo();
					LoginInvokItem.LoginInvokItemResult result = ((LoginInvokItem) resultObject).getOutput();
					if (result != null) {
						if (result.status == HttpInvokeResultCode.RESULT_OK) {
							// 同步表情
							StickersHandle.getCustomFaceList(null);
							HttpStreamCache.getInstance().ClearCacheBitmap(
									APIConfiguration.getAvatarUrlNormal(IMApplication.getCustId(), PacketType.PERSONAL));
							HttpStreamCache.getInstance().ClearCacheBitmap(
									APIConfiguration.getAvatarUrlBig(IMApplication.getCustId(), PacketType.PERSONAL));
							IMApplication.config.setUserName(result.user.nickname);
							IMApplication.setUserMobilePhone(account);
						}
						if (mILoginResultListener != null) {
							mILoginResultListener.onLoginResult(result.status, result.msg, result.user);
						}
					} else {
						if (mILoginResultListener != null) {
							mILoginResultListener.onLoginResult(-1, failReason, null);
						}
					}
				} else {
					if (mILoginResultListener != null) {
						mILoginResultListener.onLoginResult(-1, failReason, null);
					}
				}
			}
		});
	}

	public interface IRegisterCaptchaResultListener {
		void onRegisterCaptchaResult(int status, String msg, String vercode);
	}

	/**
	 * 获取注册验证码
	 *
	 * @param phoneNum
	 * @param mIRegisterCaptchaResultListener
	 *            tplid 修改找回密码 99799 注册 99798 设置支付密码 99796
	 */
	public void getRegisterCaptcha(String phoneNum, String type, final IRegisterCaptchaResultListener mIRegisterCaptchaResultListener) {
		ResendCaptchaInvokeItem item = new ResendCaptchaInvokeItem(phoneNum, type);
		KLog.d(phoneNum+"phoneNum"+"=============="+type);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY, new HttpRequestHandle.IRequestResultListener() {

			@Override
			public void onResult(Object resultObject, boolean isCache, String failReason) {
				if (resultObject != null) {

					ResendCaptchaInvokeItem.ResendCaptchaResult result = ((ResendCaptchaInvokeItem) resultObject).getOutput();
					if (result != null) {
						if (mIRegisterCaptchaResultListener != null)
							mIRegisterCaptchaResultListener.onRegisterCaptchaResult(result.status, result.msg, result.vercode);

					} else {
						if (mIRegisterCaptchaResultListener != null)
							mIRegisterCaptchaResultListener.onRegisterCaptchaResult(-1, "", "");
					}
				} else {
					if (mIRegisterCaptchaResultListener != null)
						mIRegisterCaptchaResultListener.onRegisterCaptchaResult(-1, failReason, "");
				}
			}
		});
	}

	/**
	 * 注册
	 *
	 * @param mobile_phone
	 * @param pwd
	 * @param phone_validate_code
	 * @param mILoginResultListener
	 */
	public void registerApp(final String mobile_phone, String pwd, String nickname, String phone_validate_code,
			final ILoginResultListener mILoginResultListener) {
		RegisterInvokeItme item = new RegisterInvokeItme(mobile_phone, pwd, nickname, phone_validate_code);
		HttpRequestAsyncTaskQueue.getInstance().AddTask(new HttpTextAsyncTask(item).SetRequestType(HttpRequestType.SERVER_ONLY),
				new OnHttpRequestTextListener() {

					@Override
					public void OnFail(boolean isCache, String reason) {
						IMApplication.showToast(reason);
						if (mILoginResultListener != null)
							mILoginResultListener.onLoginResult(-1, reason, null);
					}

					@Override
					public void OnProgress(long total_bytes, long downloaded_bytes) {
					}

					@Override
					public void OnSuccess(HttpInvokeItem item, boolean isCache, String cacheFilePath) {

						RegisterInvokeItme.RegisterInvokeItmeResult result = ((RegisterInvokeItme) item).getOutput();
						if (result != null) {
							if (result.mQYXUserEntity != null && result.status == HttpInvokeResultCode.RESULT_OK) {
								IMApplication.setCustName(result.mQYXUserEntity.nickname);
								IMApplication.setUserMobilePhone(mobile_phone);
							}
							if (mILoginResultListener != null)
								mILoginResultListener.onLoginResult(result.status, result.msg, result.mQYXUserEntity);

						} else {
							if (mILoginResultListener != null)
								mILoginResultListener.onLoginResult(-1, "", null);
						}
					}

				});
	}

	public interface IResetPwdResultListener {
		void onResult(int status, String msg);
	}

	/**
	 * 重置密码
	 *
	 * @param mobile_phone
	 * @param phone_validate_code
	 * @param new_password
	 * @param mIResetPwdResultListener
	 */
	public void resetPwd(String mobile_phone, String phone_validate_code, String new_password,
			final IResetPwdResultListener mIResetPwdResultListener) {
		ForgetPwdInvokeItem item = new ForgetPwdInvokeItem(mobile_phone, phone_validate_code, new_password);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY, new HttpRequestHandle.IRequestResultListener() {

			@Override
			public void onResult(Object resultObject, boolean isCache, String failReason) {
				if (resultObject != null) {
					ForgetPwdInvokeItem.ForgetPwdInvokeItemResult result = ((ForgetPwdInvokeItem) resultObject).getOutput();
					if (result != null) {
						if (mIResetPwdResultListener != null) {
							mIResetPwdResultListener.onResult(result.status, result.msg);
						}
					} else {
						if (mIResetPwdResultListener != null) {
							mIResetPwdResultListener.onResult(-1, failReason);
						}
					}
				} else {
					if (mIResetPwdResultListener != null) {
						mIResetPwdResultListener.onResult(-1, failReason);
					}
				}
			}
		});
	}

	/**
	 * 检查手机号是否注册
	 *
	 * @param mobile_phone
	 * @param mIResetPwdResultListener
	 */
	public void checkMobileIsRegister(String mobile_phone, final IResetPwdResultListener mIResetPwdResultListener) {
		CheckMobileInvokeItem item = new CheckMobileInvokeItem(mobile_phone);

		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY, new HttpRequestHandle.IRequestResultListener() {

			@Override
			public void onResult(Object resultObject, boolean isCache, String failReason) {
				if (resultObject != null) {
					CheckMobileInvokeItem.CheckMobileInvokeItemResult result = ((CheckMobileInvokeItem) resultObject).getOutput();
					if (result != null) {
						if (mIResetPwdResultListener != null) {
							mIResetPwdResultListener.onResult(result.status, result.msg);
						}
					} else {
						if (mIResetPwdResultListener != null) {
							mIResetPwdResultListener.onResult(-1, "");
						}
					}
				} else {
					if (mIResetPwdResultListener != null) {
						mIResetPwdResultListener.onResult(-1, failReason);
					}
				}
			}
		});

	}
}
